Installation¶
In [ ]:
Copied!
!pip install hyperopt
!pip install hyperopt
Sample Strategy¶
In [1]:
Copied!
# import talib.abstract as ta
from lettrade import DataFeed, Strategy, indicator as i
from lettrade.exchange.backtest import ForexBackTestAccount, let_backtest
from lettrade.indicator.vendor.qtpylib import inject_indicators
inject_indicators()
class SmaCross(Strategy):
ema1_period = 9
ema2_period = 21
def indicators(self, df: DataFeed):
# df["ema1"] = ta.EMA(df, timeperiod=self.ema1_period)
# df["ema2"] = ta.EMA(df, timeperiod=self.ema2_period)
df["ema1"] = df.close.ema(window=self.ema1_period)
df["ema2"] = df.close.ema(window=self.ema2_period)
df["signal_ema_crossover"] = i.crossover(df.ema1, df.ema2)
df["signal_ema_crossunder"] = i.crossunder(df.ema1, df.ema2)
def next(self, df: DataFeed):
if len(self.orders) > 0 or len(self.positions) > 0:
return
if df.l.signal_ema_crossover[-1]:
price = df.l.close[-1]
self.buy(size=0.1, sl=price - 0.001, tp=price + 0.001)
elif df.l.signal_ema_crossunder[-1]:
price = df.l.close[-1]
self.sell(size=0.1, sl=price + 0.001, tp=price - 0.001)
lt = let_backtest(
strategy=SmaCross,
datas="example/data/data/EURUSD_5m_0_10000.csv",
account=ForexBackTestAccount,
# plotter=None,
)
# import talib.abstract as ta
from lettrade import DataFeed, Strategy, indicator as i
from lettrade.exchange.backtest import ForexBackTestAccount, let_backtest
from lettrade.indicator.vendor.qtpylib import inject_indicators
inject_indicators()
class SmaCross(Strategy):
ema1_period = 9
ema2_period = 21
def indicators(self, df: DataFeed):
# df["ema1"] = ta.EMA(df, timeperiod=self.ema1_period)
# df["ema2"] = ta.EMA(df, timeperiod=self.ema2_period)
df["ema1"] = df.close.ema(window=self.ema1_period)
df["ema2"] = df.close.ema(window=self.ema2_period)
df["signal_ema_crossover"] = i.crossover(df.ema1, df.ema2)
df["signal_ema_crossunder"] = i.crossunder(df.ema1, df.ema2)
def next(self, df: DataFeed):
if len(self.orders) > 0 or len(self.positions) > 0:
return
if df.l.signal_ema_crossover[-1]:
price = df.l.close[-1]
self.buy(size=0.1, sl=price - 0.001, tp=price + 0.001)
elif df.l.signal_ema_crossunder[-1]:
price = df.l.close[-1]
self.sell(size=0.1, sl=price + 0.001, tp=price - 0.001)
lt = let_backtest(
strategy=SmaCross,
datas="example/data/data/EURUSD_5m_0_10000.csv",
account=ForexBackTestAccount,
# plotter=None,
)
Optimize¶
In [2]:
Copied!
# define a search space
from hyperopt import fmin, tpe, space_eval, Trials
from hyperopt import hp
lettrade_model = lt.optimize_model()
def train_model(params):
# Model
result = lettrade_model(params)
# Score
return -result["equity"]
# Hyperopt
search_space = {
"ema1_period": hp.uniformint("ema1_period", 5, 25, q=1),
"ema2_period": hp.uniformint("ema2_period", 5, 50, q=1),
}
trials = Trials()
best_params = fmin(
train_model,
search_space,
algo=tpe.suggest,
max_evals=1_000,
trials=trials,
)
# define a search space
from hyperopt import fmin, tpe, space_eval, Trials
from hyperopt import hp
lettrade_model = lt.optimize_model()
def train_model(params):
# Model
result = lettrade_model(params)
# Score
return -result["equity"]
# Hyperopt
search_space = {
"ema1_period": hp.uniformint("ema1_period", 5, 25, q=1),
"ema2_period": hp.uniformint("ema2_period", 5, 50, q=1),
}
trials = Trials()
best_params = fmin(
train_model,
search_space,
algo=tpe.suggest,
max_evals=1_000,
trials=trials,
)
100%|██████████| 1000/1000 [00:41<00:00, 24.10trial/s, best loss: -1196.58]
In [3]:
Copied!
hyperparams = space_eval(search_space, best_params)
print(best_params)
print(hyperparams)
hyperparams = space_eval(search_space, best_params)
print(best_params)
print(hyperparams)
{'ema1_period': 17.0, 'ema2_period': 8.0}
{'ema1_period': 17, 'ema2_period': 8}
Plot¶
In [4]:
Copied!
lt.plotter.heatmap(x="ema1_period", y="ema2_period", z="equity")
lt.plotter.heatmap(x="ema1_period", y="ema2_period", z="equity")
In [5]:
Copied!
lt.plotter.contour(x="ema1_period", y="ema2_period", z="equity")
lt.plotter.contour(x="ema1_period", y="ema2_period", z="equity")
Init Plotly environment¶
In [6]:
Copied!
import plotly.io as pio
pio.renderers.default = "notebook"
pio.templates.default = "plotly_dark"
import plotly.io as pio
pio.renderers.default = "notebook"
pio.templates.default = "plotly_dark"
In [7]:
Copied!
import pandas as pd
import numpy as np
def unpack(x):
if x:
return x[0]
return np.nan
# We'll first turn each trial into a series and then stack those series together as a dataframe.
df = pd.DataFrame([pd.Series(t["misc"]["vals"]).apply(unpack) for t in trials])
# Then we'll add other relevant bits of information to the correct rows and perform a couple of
# mappings for convenience
df["loss"] = [t["result"]["loss"] for t in trials]
df["trial_number"] = df.index
df["win"] = -df["loss"]
df
import pandas as pd
import numpy as np
def unpack(x):
if x:
return x[0]
return np.nan
# We'll first turn each trial into a series and then stack those series together as a dataframe.
df = pd.DataFrame([pd.Series(t["misc"]["vals"]).apply(unpack) for t in trials])
# Then we'll add other relevant bits of information to the correct rows and perform a couple of
# mappings for convenience
df["loss"] = [t["result"]["loss"] for t in trials]
df["trial_number"] = df.index
df["win"] = -df["loss"]
df
Out[7]:
| ema1_period | ema2_period | loss | trial_number | win | |
|---|---|---|---|---|---|
| 0 | 17.0 | 42.0 | -903.38 | 0 | 903.38 |
| 1 | 20.0 | 6.0 | -1096.68 | 1 | 1096.68 |
| 2 | 20.0 | 42.0 | -928.18 | 2 | 928.18 |
| 3 | 10.0 | 16.0 | -859.38 | 3 | 859.38 |
| 4 | 6.0 | 7.0 | -872.28 | 4 | 872.28 |
| ... | ... | ... | ... | ... | ... |
| 995 | 16.0 | 17.0 | -813.68 | 995 | 813.68 |
| 996 | 18.0 | 7.0 | -1147.08 | 996 | 1147.08 |
| 997 | 14.0 | 8.0 | -1171.68 | 997 | 1171.68 |
| 998 | 15.0 | 12.0 | -1039.58 | 998 | 1039.58 |
| 999 | 8.0 | 13.0 | -832.18 | 999 | 832.18 |
1000 rows × 5 columns
Type 1¶
In [8]:
Copied!
from plotly import express as px
fig = px.scatter(df, x="trial_number", y="win")
fig.show()
from plotly import express as px
fig = px.scatter(df, x="trial_number", y="win")
fig.show()
Type 2¶
In [9]:
Copied!
import plotly.express as px
fig = px.density_contour(
df,
x="ema1_period",
y="ema2_period",
z="win",
histfunc="max",
)
fig.update_traces(contours_coloring="fill", contours_showlabels=True)
fig.show()
import plotly.express as px
fig = px.density_contour(
df,
x="ema1_period",
y="ema2_period",
z="win",
histfunc="max",
)
fig.update_traces(contours_coloring="fill", contours_showlabels=True)
fig.show()
Type 3¶
In [10]:
Copied!
import plotly.express as px
fig = px.density_heatmap(
df,
x="ema1_period",
y="ema2_period",
z="win",
nbinsx=20,
nbinsy=40,
histfunc="max",
color_continuous_scale="Viridis",
)
fig.show()
import plotly.express as px
fig = px.density_heatmap(
df,
x="ema1_period",
y="ema2_period",
z="win",
nbinsx=20,
nbinsy=40,
histfunc="max",
color_continuous_scale="Viridis",
)
fig.show()